package com.droid.library.widget.recyclerview;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import java.io.Serializable;

/**
 * RecyclerView基类
 * @param <T>
 */
public abstract class BaseRecyclerAdapter<T extends Serializable>
        extends BaseDataRecyclerAdapter<T> {

    private static final int BASE_ITEM_TYPE_HEADER = 100000;
    private static final int BASE_ITEM_TYPE_FOOTER = 200000;
    protected static final int ITEM_TYPE_NORMAL = 300000;


    private SparseArrayCompat<View> mHeaderList = new SparseArrayCompat<>();
    private SparseArrayCompat<View> mFooterList = new SparseArrayCompat<>();

    protected Activity mActivity;
    protected Fragment mFragment;

    public BaseRecyclerAdapter(Activity activity){
        mActivity = activity;
    }
    public BaseRecyclerAdapter(Fragment fragment){
        mFragment = fragment;
        mActivity = mFragment.getActivity();
    }
    private boolean isHeaderView(int position) {
        return position < getHeadersCount();
    }

    private boolean isFooterView(int position) {
        return position >= getHeadersCount() + getRealItemCount();
    }


    public void addHeaderView(View view) {
        mHeaderList.put(mHeaderList.size() + BASE_ITEM_TYPE_HEADER, view);
    }

    public void addFooterView(View view) {
        mFooterList.put(mFooterList.size() + BASE_ITEM_TYPE_FOOTER, view);
    }

    public int getHeadersCount() {
        return mHeaderList.size();
    }

    public int getFootersCount() {
        return mFooterList.size();
    }

    @Override
    public final BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderList.get(viewType) != null) {
            if(mFragment != null){
                return new HeaderViewHolder(mHeaderList.get(viewType),mFragment);
            }else{
                return new HeaderViewHolder(mHeaderList.get(viewType),mActivity);
            }
        } else if (mFooterList.get(viewType) != null) {
            if(mFragment != null){
                return new FooterViewHolder(mFooterList.get(viewType),mFragment);
            }else{
                return new FooterViewHolder(mFooterList.get(viewType),mActivity);
            }
        }
        return onCreateRealViewHolder(parent,viewType);
    }

    @Override
    public final int getItemViewType(int position) {
        if (isHeaderView(position)) {
            return mHeaderList.keyAt(position);
        } else if (isFooterView(position)) {
            return mFooterList.keyAt(position - getHeadersCount() - getRealItemCount());
        }
        return getRealItemViewType(position - getHeadersCount());
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        int realPosition = position - getHeadersCount();
        if(realPosition >= 0 && realPosition < getRealItemCount()){
            holder.onBindViewHolder(position, getRealItemAtPosition(realPosition));
        }else{
            holder.onBindViewHolder(position, null);
        }
    }



    @Override
    public final int getItemCount() {
        return getHeadersCount() + getFootersCount() + getRealItemCount();
    }

    public final T getItem(int position){
        return getRealItemAtPosition(position - getHeadersCount());
    }


    protected abstract BaseViewHolder onCreateRealViewHolder(ViewGroup parent, int viewType);

    protected int getRealItemViewType(int position){
        return ITEM_TYPE_NORMAL;
    }

    /************************************************************************/
    /**************** 开始处理GridView和瀑布流方式的Header和Footer **************/
    /************************************************************************/
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {

        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager manager = (GridLayoutManager) layoutManager;
            final GridLayoutManager.SpanSizeLookup lookup = manager.getSpanSizeLookup();
            manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int viewType = getItemViewType(position);
                    if (mHeaderList.get(viewType) != null) {
                        return manager.getSpanCount();
                    } else if (mFooterList.get(viewType) != null) {
                        return manager.getSpanCount();
                    }
                    if (lookup != null) {
                        return lookup.getSpanSize(position);
                    }
                    return 1;
                }
            });
            manager.setSpanCount(manager.getSpanCount());
        }

        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public void onViewAttachedToWindow(BaseViewHolder holder) {

        int position = holder.getLayoutPosition();
        if (isHeaderView(position) || isFooterView(position)) {
            ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
            if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) {
                StaggeredGridLayoutManager.LayoutParams p =
                        (StaggeredGridLayoutManager.LayoutParams) lp;
                p.setFullSpan(true);
            }
        }
        super.onViewAttachedToWindow(holder);
    }

    /************************************************************************/
    /**************** 结束处理GridView和瀑布流方式的Header和Footer **************/
    /************************************************************************/

}
